/**
 * 角色管理
 */
layui.config({
  base: '/'
}).extend({
  flying: 'flying'
}).define(['index', 'flying'], function (exports) {  //先加载index模块，里面配置了全局的入口
  const admin = layui.admin,
      flying = layui.flying
      ,form = layui.form
      ,table = layui.table
      ,layer = layui.layer
      ,dropdown = layui.dropdown;

  flying.table({
    elem: '#LAY-role-table',
    url: '/system/role/page', //数据接口
    cols: [
      {type: 'numbers', fixed: 'left', title: '序号'}
      , {field: 'roleId', title: '角色编码', minWidth: 100,}
      , {field: 'roleName', title: '角色名称', minWidth: 110}
      , {field: 'status', title: '状态', minWidth: 90, templet: '#statusSwitchTpl', unresize: true}
      , {field: 'updatedBy', title: '修改人', minWidth: 90}
      , {field: 'updatedTime', title: '修改时间', templet: '<div>{{ layui.util.toDateString(d.updatedTime, "yyyy-MM-dd HH:mm:ss") }}</div>',  minWidth: 170, sort: true}
      , {title: '操作', width: 160, align: 'center', fixed: 'right', toolbar: '#role-table'}
    ]
  });

  const role = {
    // 保存
    handlesSave: function (index, layero) {
      let iframeWindow = window['layui-layer-iframe' + index]
          , submitID = 'LAY-role-front-submit'
          , submit = layero.find('iframe').contents().find('#' + submitID);

      //监听提交
      iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
        let field = data.field; //获取提交的字段

        //提交 Ajax 成功后，静态更新表格中的数据
        admin.req({
          url: '/system/role/save',
          type: 'post',
          contentType: 'application/json;charset=utf-8',
          dataType: 'json',
          data: JSON.stringify(field),
          done: function (res) {
            //执行成功
            layer.msg(res.msg, {icon: 1});
            table.reload('LAY-role-table'); //数据刷新
            layer.close(index); //关闭弹层
          }
        });
      });

      submit.trigger('click');
    },
    // 删除
    handleDelete: function (index, id) {
      //提交 Ajax 成功后，更新表格中的数据
      admin.req({
        url: '/system/role/deleteById/' + id
        , type: 'get'
        , done: function (res) {
          //执行成功
          layer.msg(res.msg, {icon: 1});
          table.reload('LAY-role-table'); //数据刷新
          layer.close(index); //关闭弹层
        }
      });
    },
    // 权限分配
    handlePerms: function (index, layero) {
      let iframeWindow = window['layui-layer-iframe' + index]
          , submitID = 'LAY-role-resource-front-submit'
          , submit = layero.find('iframe').contents().find('#' + submitID);

      //监听提交
      iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
        let field = data.field; //获取提交的字段
        let arr = [];
        if (field.resIds) {
          let strs = field.resIds.split(',');
          for (let i = 0; i < strs.length; i++) {
            let ob = {};
            ob.roleId = field.roleId;
            ob.resId = strs[i];

            arr.push(ob);
          }
        }

        //提交 Ajax 成功后，更新表格中的数据
        admin.req({
          url: '/system/role/perms/save'
          , type: 'post'
          , contentType: 'application/json;charset=utf-8'
          , data: JSON.stringify(arr)
          , done: function (res) {
            //执行成功
            layer.msg(res.msg, {icon: 1});
            table.reload('LAY-role-table'); //数据刷新
            layer.close(index); //关闭弹层
          }
        });
      });

      submit.trigger('click');
    }
  }

  //对外暴露的接口
  exports('role', role);
});